﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MsiCustomize
{
    [Serializable]
    public class MsiPropertyCollection : ObservableCollection<MsiPropertyItem>
    {
        public MsiPropertyItem this[string key]
        {
            get
            {
                int Index;
                if (Lookup(key, out Index))
                {
                    return this[Index];
                }
                return null;
            }
            set  
            {
                if (String.IsNullOrEmpty(value.Name))
                {
                    throw new ArgumentNullException("Name", "Property name cannot be null or empty.");
                }
                int Index;
                if (Lookup(key, out Index))
                {
                    this[Index] = value;
                    return;
                }
                this.Add(value);
            }
        }

        public int IndexOf(string key)
        {
            int Index;
            Lookup(key, out Index);
            return Index;
        }

        private bool Lookup(string key, out int index)
        {
            index = -1;
            for (int i = 0; i < this.Count - 1; i++)
            {
                if (this[i].Name == key)
                {
                    index = i;
                    return true;
                }
            }
            return false;
        }
    }
}
